package com.per.note.ui.login;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.per.note.R;
import com.per.note.db.ResultMessage;
import com.per.note.db.UserDao;
import com.per.note.db.url;
import com.per.note.utils.HttpUtil;
import com.per.note.utils.JsonTools;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class RegisterActivity extends AppCompatActivity {

//    PersonDBdao persondbdao;
    UserDao userDao = new UserDao(this);
    private EditText mEditTextName;// 账号
    private EditText mEditTextPwd1;// 密码
    private EditText mEditTextPwd2;// 密码
    private static String JsonString;

    private String name;
    private String pwd1;
    private String pwd2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        setContentView(R.layout.activity_register);

        mEditTextName = (EditText) this.findViewById(R.id.et_register_username);
        mEditTextPwd1 = (EditText) this.findViewById(R.id.et_register_new_pwd);
        mEditTextPwd2 = (EditText) this.findViewById(R.id.et_register_confirm_pwd);
    }

    public void onClick(View v) {
        switch(v.getId()){
            case R.id.bt_register_ok:
                name = mEditTextName.getText().toString();
                pwd1 = mEditTextPwd1.getText().toString().trim();
                pwd2 = mEditTextPwd2.getText().toString().trim();

                if (name.equals("")) {
                    Toast.makeText(getApplicationContext(), "账户名不能为空！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                if (pwd1.equals("")) {
                    Toast.makeText(getApplicationContext(), "密码不能为空！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                if (!pwd1.equals(pwd2)) {
                    Toast.makeText(getApplicationContext(), "确认密码不同！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                userDao.add(name,pwd2);

                url url = new url();
                String path = url.getUrl()+"/AccountServer/registerAction";
                String path0 = null;
                try {
                    path0 = path +
                        //"?userid=" + URLEncoder.encode(""+id,"utf-8").toString() +
                        "?name=" +  URLEncoder.encode(""+name,"utf-8").toString() +
                        "&password=" +  URLEncoder.encode(""+pwd2,"utf-8").toString();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                new RegisterActivity.MyTask().execute(path0);

                Toast.makeText(getApplicationContext(), "注 册 成 功 ！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
//                intent.putExtra("name", name);
//                intent.putExtra("password",pwd2);
                // 传值 帐户名
                this.startActivity(intent);
//                finish();
                break;
            case R.id.bt_register_cancel:
                finish();
                break;
        }
    }

    class MyTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... arg0) {
            String jsonString = HttpUtil.getJsonContent(arg0[0]);
            return jsonString;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            JsonString = result;
            ResultMessage resultm = JsonTools.getString("ResultMessage", JsonString);
            if(resultm.getResultCode()==1){
                Toast.makeText(RegisterActivity.this, resultm.getResultMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}
